using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class HitEffect : MonoBehaviour
{
	public float duration = 1;
	public bool isTemplate = true;

	protected static Dictionary<string, GameObject> effectList = new Dictionary<string, GameObject>();


	void Awake()
	{
		if(isTemplate)
			gameObject.SetActive(false);

		if(effectList.ContainsKey(gameObject.name) == false)
		{
			effectList[gameObject.name] = gameObject;
		}
	}

	public virtual void OnHit(Character sender, Character receiver, InjuryInformation info)
	{
		GameObject template = effectList.ContainsKey(gameObject.name)? effectList[gameObject.name]: gameObject;

		GameObject clone = GameObject.Instantiate(template) as GameObject;
		HitEffect effect = clone.GetComponent<HitEffect>();
		effect.StartEffect(sender, receiver, info);
	}

	protected void StartEffect(Character sender, Character receiver, InjuryInformation info)
	{
		isTemplate = false;
		gameObject.SetActive(true);
		transform.position = receiver.transformRef.position;

		transform.rotation = Quaternion.Euler( new Vector3 (transform.rotation.eulerAngles.x, 
			             receiver.transformRef.rotation.eulerAngles.y,
			             transform.rotation.eulerAngles.z));
                
        /*
        transform.eulerAngles = new Vector3(180-sender.weaponFlashObj.transform.eulerAngles.x,
                                            270-sender.weaponFlashObj.transform.eulerAngles.y,
                                            270-sender.weaponFlashObj.transform.eulerAngles.z);
                                            */

		Invoke("Disappear", duration);
	}

	void Disappear()
	{
		GameObject.Destroy(gameObject);
	}
}

